package com.tmpt.controller.springmvc.handlers;

import com.tmpt.controller.springmvc.PageUtils;
import com.tmpt.dto.Result;
import com.tmpt.utils.WebUtils;
import com.tmptSource.dto.OwCommiteeRecordDto;
import com.tmptSource.dto.OwDoorRespInfoDto;
import com.tmptSource.dto.OwIssueDeclareDto;
import com.tmptSource.dto.PageDto;
import com.tmptSource.service.intf.IOwDoorRespInfoService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author helen
 * @title: OwDoorRespInfoAction
 * @projectName bird
 * @description: TODO
 * @date 2019/11/1020:12
 */
@Controller
@RequestMapping("/springmvc/doorResponsibility")
public class OwDoorRespInfoAction {
    private Logger log = Logger.getLogger(this.getClass());

    @Resource
    private IOwDoorRespInfoService owDoorRespInfoService;

    @RequestMapping(value = "/queryDoorRespList")
    @ResponseBody
    public Result queryDoorRespList(OwDoorRespInfoDto owDoorRespInfoDto, PageDto pageDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwDoorRespInfoDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }
            //查询参数
            String userId = WebUtils.getUserId(session);
            owDoorRespInfoDto.setCreatemanId(userId);

            pageDto = PageUtils.resetPageDto(pageDto);

            //调用远程服务
            result =  owDoorRespInfoService.pagination(owDoorRespInfoDto, pageDto);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
    }

    @RequestMapping(value = "/queryDoorRespListWithoutLogin")
    @ResponseBody
    public Result queryDoorRespListWithoutLogin(OwDoorRespInfoDto owDoorRespInfoDto, PageDto pageDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwDoorRespInfoDto> result = new Result();
        try {
            pageDto = PageUtils.resetPageDto(pageDto);

            //调用远程服务
            result =  owDoorRespInfoService.pagination(owDoorRespInfoDto, pageDto);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
    }

    @RequestMapping(value = "/queryDoorRespAdvListWithoutLogin")
    @ResponseBody
    public Result queryDoorRespAdvListWithoutLogin(OwDoorRespInfoDto owDoorRespInfoDto, PageDto pageDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwDoorRespInfoDto> result = new Result();
        try {
            pageDto = PageUtils.resetPageDto(pageDto);
            Map<String,Object> paramMap = new HashMap<String, Object>();
            owDoorRespInfoDto.setAdvBean("advBean");
            paramMap.put("bean", owDoorRespInfoDto);
            //调用远程服务
            result =  owDoorRespInfoService.pagination(owDoorRespInfoDto, pageDto, paramMap);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
    }

    /**
     * 从二维码(QRCode:quick response code)页面直接插入记录，不做登录校验,为APP专用
     * @param owDoorRespInfoDto
     * @param request
     * @return
     */
    //2019-11-24 新增图片功能 helen
    @RequestMapping(value = "/addDoorRespByQRCode")
    @ResponseBody
    public Result addDoorRespByQRCode( OwDoorRespInfoDto owDoorRespInfoDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwDoorRespInfoDto> result = new Result();
        try {
            //插入参数
            owDoorRespInfoDto.setCreatemanId("0");
            owDoorRespInfoDto.setCreateman("0");
            owDoorRespInfoDto.setCompanyId("0");
            //调用远程服务
            result =  owDoorRespInfoService.addDataByQRCode(owDoorRespInfoDto);
            OwDoorRespInfoDto owD =  result.getResultObj();

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
    }

    /**
     * 生成一条只有ID和randomKey的空记录，delete_flag=1默认删除状态,为APP专用
     * @param owDoorRespInfoDto
     * @param request
     * @return
     */
    @RequestMapping(value = "/insertDoorResp")
    @ResponseBody
    public Result insertDoorResp( OwDoorRespInfoDto owDoorRespInfoDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwDoorRespInfoDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }

            //插入参数
            String userId = WebUtils.getUserId(session);
            String username = WebUtils.getUsername(session);
            String companyId = WebUtils.getCompanyId(session);
            owDoorRespInfoDto.setCreatemanId(userId);
            owDoorRespInfoDto.setCreateman(username);
            owDoorRespInfoDto.setCompanyId(companyId);
            //调用远程服务
            result =  owDoorRespInfoService.addData(owDoorRespInfoDto);

//            OwDoorRespInfoDto owD =  result.getResultObj();
//            owDoorRespInfoService.deleteById(Long.valueOf(owD.getId()),username);

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
    }


    /**
     *登录不校验，APP专用
     * @param owDoorRespInfoDto
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryDoorRespByKey")
    @ResponseBody
    public Result queryDoorRespByKey( OwDoorRespInfoDto owDoorRespInfoDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwDoorRespInfoDto> result = new Result();
        try {
//            if(!WebUtils.isLoginSuccess(session)){
//                return result.fault("请先登录");
//            }

            String randomKey = owDoorRespInfoDto.getRandomKey();
            //通过randomKey获取ID
            long id = owDoorRespInfoService.selectIdByKey(randomKey);

            //调用远程服务
            result =  owDoorRespInfoService.selectById(id);
            return result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
    }



    /**
     * 编辑页面登录不校验，APP专用
     * @param owDoorRespInfoDto
     * @param request
     * @return
     */
    @RequestMapping(value = "/editDoorResp")
    @ResponseBody
    public Result editDoorResp( OwDoorRespInfoDto owDoorRespInfoDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwDoorRespInfoDto> result = new Result();
        try {
            /**
             * 编辑页面登录不校验，APP专用
             */
            /*if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }*/

            String randomKey = owDoorRespInfoDto.getRandomKey();
            //通过randomKey获取ID
            long id = owDoorRespInfoService.selectIdByKey(randomKey);
            owDoorRespInfoDto.setId(String.valueOf(id));
            //插入参数
            owDoorRespInfoDto.setModifymanId("0");
            owDoorRespInfoDto.setModifyman("0");

            //调用远程服务
            result =  owDoorRespInfoService.editData(owDoorRespInfoDto);
            return result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault(e.getLocalizedMessage());
        }finally {
        }
    }


}
